package com.akk.module.hkpay.util;

import com.akk.utils.QRCodeEvents;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;

public class QRUtils {

    public static String makeQR(String url,int w,int h){
        w = w == 0 ? 300 : w;
        h = h == 0 ? 300 : h;
        BufferedImage image = QRCodeEvents.createQRCode(url,w,h);
        byte[] data = null;
        ByteArrayOutputStream bs = new ByteArrayOutputStream();

        ImageOutputStream imOut;
        InputStream is = null;
        try {
            imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(image, "png",imOut);
            is = new ByteArrayInputStream(bs.toByteArray());
            data = new byte[is.available()];
            is.read(data);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

}
